package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.SkuDetailFeignClient;
import com.atguigu.gmall.feign.product.ProductSkuDetailFeignClient;
import com.atguigu.gmall.product.vo.SkuDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;

@Controller
@RequestMapping
public class ItemController {
    @Autowired
    SkuDetailFeignClient skuDetailFeignClient;

    @Autowired
    ProductSkuDetailFeignClient productSkuDetailFeignClient;

    @GetMapping("/{skuId}.html")
    public String itemPage(@PathVariable Long skuId, Model model){
        Result<SkuDetailVo> result = skuDetailFeignClient.getSkuDetails(skuId);
        SkuDetailVo detailVo = result.getData();

        // 1.分类视图
        model.addAttribute("categoryView",detailVo.getCategoryView());

        // 2.SKu信息(基本信息和图片列表)
        model.addAttribute("skuInfo",detailVo.getSkuInfo());

        // 3.实时价格
        BigDecimal price = productSkuDetailFeignClient.getPrice(skuId).getData();
        model.addAttribute("price",price);

        // 4.所有的销售属性结合
        model.addAttribute("spuSaleAttrList",detailVo.getSpuSaleAttrList());

        // 5.ValuesSkuJson
        model.addAttribute("valuesSkuJson",detailVo.getValuesSkuJson());

        // 6.Sku的规格：平台属性
        return "item/index";
    }
}
